// pages/material/pages/terms/index.js
let timeInterval;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        info: {
            id: 1,
            title: '',
            html: '',
        },
        disabled: true,
        recordTime: 2, //倒计时间(默认10s)
        isAgree: false, //相关协议是否同意
        checkbox_animate: false, //震动效果
        curr_id: '', //当前id
        curr_index: '', //当前索引
    },
    //计时器
    record_Time: function () {
        clearInterval(timeInterval)
        var that = this;
        // 播放时长记录 每秒刷新
        timeInterval = setInterval(() => {
            that.data.recordTime -= 1
            const recordTime = that.data.recordTime
            if (recordTime <= -1) {
                that.setData({
                    disabled: false
                })
                clearInterval(timeInterval)
            } else {
                that.setData({
                    recordTime
                })
            }
        }, 1000)
    },
    //协议勾选
    checkboxChange: function (e) {
        console.log(e.detail.value.length)
        this.setData({
            isAgree: !!e.detail.value.length
        })
    },
    //确认
    to_submit: function (e) {
        var that = this;
        let isAgree = that.data.isAgree
        if (isAgree == false) {
            wx.showToast({
                title: '请勾选我已阅读并同意以上内容',
                icon: 'none',
                mask: true,
                duration: 1500
            });
            that.setData({
                checkbox_animate: true
            })
            setTimeout(() => {
                that.setData({
                    checkbox_animate: false
                })
            }, 1500)
            return false
        }
        let curr_index = that.data.curr_index;
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2]; //上一个页面
        prevPage.setData({
            [`terms_info[${curr_index}].approved`]: true,
        })
        wx.navigateBack();
    },
    //文件下载
    linktap(e) {
        if (e.detail.type == 'doc' || e.detail.type == 'docx' || e.detail.type == 'xls' || e.detail.type == 'xlsx' || e.detail.type == 'ppt' || e.detail.type == 'pdf' || e.detail.type == 'pptx') {
            wx.showLoading({
                title: '加载中',
            })
            // 下载 doc 文件
            wx.downloadFile({
                url: e.detail.href,
                success(res) {
                    wx.hideLoading()
                    wx.openDocument({
                        showMenu: true,
                        filePath: res.tempFilePath
                    })
                },
                fail(err) {
                    wx.hideLoading()
                    wx.showModal({
                        title: '失败',
                        content: err.errMsg,
                        showCancel: false
                    })
                }
            })
        }
    },
    find_read_agreement:function(options){
        let params = {}
        params.id= options.record_id;
        getApp()._post_form('data/api.auth.order/find_read_agreement', params, (result) => {
            let rt_data = result.data.data
            if( options.id == 1){
                // 
                let html = rt_data.read_agreement5;
                this.setData({
                    'info.id':options.id,
                    'info.title': '《公证处公证申请表》',
                    'info.html': html,
                })
                wx.setNavigationBarTitle({
                    title: '《公证处公证申请表》'
                })
            }
            else if( options.id == 2){
                let html = rt_data.read_agreement4;
                this.setData({
                    'info.id':options.id,
                    'info.title': '《公证受理通知单回执》',
                    'info.html': html,
                })
                wx.setNavigationBarTitle({
                    title: '《公证受理通知单回执》'
                })
            }
            else if( options.id == 3){
                let html = rt_data.read_agreement3;
                this.setData({
                    'info.id':options.id,
                    'info.title': '《涉外民事类公证告知书》',
                    'info.html': html,
                })
                wx.setNavigationBarTitle({
                    title: '《涉外民事类公证告知书》'
                })
            }
            else if( options.id == 4){
                let html = rt_data.read_agreement2;
                this.setData({
                    'info.id':options.id,
                    'info.title': '《询问记录》',
                    'info.html': html,
                })
                wx.setNavigationBarTitle({
                    title: '《询问记录》',
                })
            }
            else if( options.id == 5){
                let html = rt_data.read_agreement1;
                this.setData({
                    'info.id':options.id,
                    'info.title': '《送达回证》',
                    'info.html': html,
                })
                wx.setNavigationBarTitle({
                    title: '《送达回证》',
                })
            }
        //     this.setData({
        //         terms_info: result.data,
        //     });
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        this.setData({
            curr_id: options.id, //当前id
            curr_index: options.index, //当前索引
        })
        this.find_read_agreement(options);
    },
    // 富文本出错事件
    error(e) {
        console.error(e);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
       // this.record_Time(); //开始计时
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        clearInterval(timeInterval)
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        clearInterval(timeInterval)
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})